const app = getApp()
Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0
        })
      }
    },
  },
  data: {
    icons: [{
        img: '/images/icon1.png',
        txt: '院校'
      },
      {
        img: '/images/icon9.png',
        txt: '证书'
      },
      {
        img: '/images/icon2.png',
        txt: '专业'
      },
      {
        img: '/images/icon3.png',
        txt: '历年数据'
      },
    ],
    icons1: [{
        img: '/images/icon4.png',
        txt: '专插本'
      },
      {
        img: '/images/icon5.png',
        txt: '热度排名'
      },
      {
        img: '/images/icon6.png',
        txt: '地区微群'
      },
      // {
      //   img: '/images/icon7.png',
      //   txt: '教材课程'
      // },
      {
        img: '/images/icon10.png',
        txt: '咨询老师'
      },

    ],
    nodata: true,
    schools: [],
    news: [],
    jumpUrl: ''
  },
  methods: {
    navToNews(e) {
      let jumpUrl = e.currentTarget.dataset.url
      app.globalData.jumpUrl = jumpUrl
      // console.log('jumpUrl', jumpUrl);
      wx.navigateTo({
        url: '/pages/newslists/newsDetail/newsDetail',
      })
    },
    editUserInfo(e) {
      wx.navigateTo({
        url: '/pages/fill/fillform/fillform?edit=1',
      })
    },
    navTo() {
      wx.navigateTo({
        url: '/pages/fill/fill',
      })
    },
    navToSearch() {
      wx.navigateTo({
        url: '/pages/search/search'
      })
    },
    naToAddInfo() {
      wx.navigateTo({
        url: '/pages/fill/fillform/fillform',
      })
    },
    clickIcon1(e) {
      let index = e.currentTarget.dataset.index;
      if (index == 0) {
        wx.navigateTo({
          url: '/pages/school/school',
        })
      }
      if (index == 1) {
        wx.navigateTo({
          url: '/pages/certificate/certificate',
        })
      }
      if (index == 2) {
        wx.navigateTo({
          url: '/pages/major/major',
        })
      }
      if (index == 3) {
        wx.navigateTo({
          url: '/pages/history/history',
        })
      }


    },
    clickIcon2(e) {
      let index = e.currentTarget.dataset.index;
      if (index == 0) {
        // wx.showToast({
        //   title: '暂未开放',
        //   icon: 'error'
        // })
        wx.navigateTo({
          url: '/pages/majorben/majorben',
        })
      }
      if (index == 1) {
        wx.navigateTo({
          url: '/pages/hotrank/hotrank',
        })
      }
      if (index == 2) {
        wx.navigateTo({
          url: '/pages/chatgroup/chatgroup',
        })
      }
      if (index == 3) {
        wx.showToast({
          title: '暂未开放',
          icon: 'error'
        })
      }
      if (index == 3) {
        
        // wx.navigateTo({
        //   url: '/pages/course/course',
        // })
        wx.navigateTo({
          url: '/pages/bar/me/aboutus/aboutus',
        })
      }
    },
    majorInfo(e) {
      // console.log(e);
      let id = e.currentTarget.id;
      wx.navigateTo({
        url: '/pages/majorinfo/majorinfo?id=' + id,
      })
    },
    schoolInfo(e) {
      // console.log(e);
      let id = e.currentTarget.id;
      wx.navigateTo({
        url: '/pages/schoolinfo/schoolinfo?id=' + id,
      })
    },
    majorList(e) {
      wx.navigateTo({
        url: '/pages/major/major',
      })
    },
    schoolList(e) {
      wx.navigateTo({
        url: '/pages/school/school',
      })
    },
    newsList(e) {
      wx.navigateTo({
        url: '/pages/newslists/newslists',
      })
    },
  },
  created: function () {
    // console.log('[my-component] created')
  },
  attached: function () {
    let that = this;
    // console.log('app.globalData', app.globalData.user);
    // console.log('request');

    app.globalData.wxRequest.requestGet('major/getPopularMajor.do', {}).then(res => {
      // console.log(res);
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
      that.setData({
        hotmajor: res.data,
      })
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })

    app.globalData.wxRequest.requestGet('data/getInformation.do', {
      pageSize: 6
    }).then(res => {
      // console.log(res);
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
      let news = res.data;
      that.setData({
        news: news,
      })
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })

    wx.login({
      success: res => {
        // console.log('login',res);
        app.globalData.wxRequest.request('user/login.do', {
          code: res.code
        }).then(res1 => {
          if (res1.code == '201') {
            wx.showToast({
              title: res.msg,
              icon: 'error'
            })
          }
          // console.log('login_code',res1);
          app.globalData.openId = res1.data.openId
          app.globalData.isSignUp = res1.data.isSignUp
          app.globalData.sessionKey = res1.data.sessionKey
          app.globalData.user = res1.data
          let openId = app.globalData.openId
          // console.log('openId', app.globalData.openId);
          if (!openId) {
            return
          }
          app.globalData.wxRequest.requestGet('volunteer/getUserDetail.do', {
            openId: openId
          }).then(res2 => {
            if (res2.code == '201') {
              wx.showToast({
                title: res2.msg,
                icon: 'error'
              })
            }
            that.setData({
              userInfo: res2.data,
            }, function () {
              that.setData({
                nodata: false
              })
            })
          }).catch(err => {
            wx.showToast({
              title: '请求失败',
              icon: 'error'
            })
          })
          app.globalData.wxRequest.requestGet('volunteer/getRecommendedColleges.do', {
            id: app.globalData.user.id
          }).then(res => {
            // console.log(res);
            if (res.code == '201') {
              wx.showToast({
                title: res.msg,
                icon: 'error'
              })
            }
            that.setData({
              schools: res.data,
            })
          }).catch(err => {
            console.log(err);
            wx.showToast({
              title: '请求失败',
              icon: 'error'
            })
          })
        }).catch(err => {
          wx.showToast({
            title: '请求失败',
            icon: 'error'
          })
        })
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // console.log('[my-component] attached')
  },
  ready: function () {
    // console.log('[my-component] ready')
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})